from app.extensions import db
import datetime
from werkzeug.security import generate_password_hash, check_password_hash

from flask_login import UserMixin


class Setting(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(64))
    name = db.Column(db.String(16))
    bank = db.Column(db.String(16))
    card_number = db.Column(db.String(128))


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20))
    mobile = db.Column(db.String(11))
    is_admin = db.Column(db.Boolean(), default=False)
    password_hash = db.Column(db.String(128))

    loan = db.relationship('Loan', uselist=False)

    timestamp = db.Column(db.DateTime(), default=datetime.datetime.utcnow)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)


class Loan(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    count = db.Column(db.Float())
    status = db.Column(db.Boolean())

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User')

    timestamp = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    deadline = db.Column(db.Date())


class AddressBook(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
    mobile = db.Column(db.String(11))
    pid = db.Column(db.Integer)
    timestamp = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
